<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('修改角色')" />
	<th:block th:include="include :: ztree-css" />
</head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-role-edit" th:object="${role}">
			<input id="roleId" name="roleId" type="hidden" th:field="*{roleId}"/>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">所属部门：</label>
				<div class="col-sm-8">
					<select name="deptId" class="form-control m-b" th:with="depts=${deptList}" required>
						<option value="">请选择</option>
						<option th:each="dept : ${depts}" th:text="${dept.deptName}" th:value="${dept.deptId}" th:field="*{deptId}"></option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">角色名称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="roleName" id="roleName" th:field="*{roleName}" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">角色简称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="roleKey" id="roleKey" th:field="*{roleKey}" required>
					<span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 控制器中定义的角色简称，如：@RequiresRoles("")</span>
				</div>
			</div>

			<div class="form-group">
				<label class="col-sm-3 control-label">角色职级：</label>
				<div class="col-sm-8">
					<select name="roleRank" class="form-control m-b" th:with="type=${@dict.getType('sys_role_rank')}">
						<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{roleRank}"></option>
					</select>
				</div>
			</div>

			<div class="form-group">
				<label class="col-sm-3 control-label is-required">显示顺序：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="roleSort" id="roleSort" th:field="*{roleSort}" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">状态：</label>
				<div class="col-sm-8">
					<label class="toggle-switch switch-solid">
                        <input type="checkbox" id="status" th:checked="${role.status == '0' ? true : false}">
                        <span></span>
                    </label>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">备注：</label>
				<div class="col-sm-8">
					<input id="remark" name="remark" class="form-control" type="text" th:field="*{remark}">
				</div>
			</div>

<!--			<div class="form-group">
				<label class="col-sm-3 control-label">wms仓库编号（英文逗号分隔）：</label>
				<div class="col-sm-8">
					<input id="wmsCode" name="wmsCode" class="form-control" type="text" th:field="*{wmsCode}">
				</div>
			</div>-->

			<div class="form-group" style="display: none">

				<label class="col-sm-3 control-label ">外部系统权限地区：</label>
				<input type="hidden" id="areaIds" name="areaIds">
				<div class="col-sm-8">
					<div class='input-group'>
						<textarea name="titles" id="titles" class="form-control"  readonly></textarea>
						<span  id="selectDictTree" class='input-group-addon input-sm' onclick='selectDictTree2()'>
                                <i class='fa fa-search' >选择</i>
                    	</span>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">菜单权限</label>
				<div class="col-sm-8">
					<div id="menuTrees" class="ztree"></div>
				</div>
			</div>
		</form>
	</div>
	<th:block th:include="include :: footer" />
	<th:block th:include="include :: ztree-js" />
	<script th:inline="javascript">
	     $(function() {
			var url = ctx + "system/menu/roleMenuTreeData?roleId=" + $("#roleId").val();
			var options = {
				id: "menuTrees",
		        url: url,
		        check: { enable: true },
		        expandLevel: 0
		    };
			$.tree.init(options);
		});

         $("#titles").val([[${role.areaNames}]]);

         // 选择字典处理函数
         function selectDictTree2() {
             // alert("awefwewfew");
             var url = ctx + "hr/area/selectTreeCheckBox?roleId="+'[[${role.roleId}]]';
             var options = {
                 title: '调动人姓名',
                 width: "380",
                 url: url,
                 callBack: doSubmit
             };
             $.modal.openOptions(options);
         }

         function doSubmit(index, layero){

             var body = layer.getChildFrame('body', index);
             var areaId = body.find('#ids').val();
             var title = body.find('#titles').val();

             layer.close(index);
             $("#areaIds").val(areaId);
             $("#titles").val(title);
         }

		$("#form-role-edit").validate({
			onkeyup: false,
			rules:{
				roleName:{
					remote: {
		                url: ctx + "system/role/checkRoleNameUnique",
		                type: "post",
		                dataType: "json",
		                data: {
							"roleId": function() {
							    return $("#roleId").val();
							},
							"roleName": function() {
							    return $.common.trim($("#roleName").val());
							}
		                },
		                dataFilter: function(data, type) {
		                	return $.validate.unique(data);
		                }
		            }
				},
				roleKey:{
					remote: {
		                url: ctx + "system/role/checkRoleKeyUnique",
		                type: "post",
		                dataType: "json",
		                data: {
							"roleId": function() {
								return $("#roleId").val();
							},
							"roleKey": function() {
							    return $.common.trim($("#roleKey").val());
							}
		                },
		                dataFilter: function(data, type) {
		                	return $.validate.unique(data);
		                }
		            }
				},
				roleSort:{
					digits:true
				},
			},
			messages: {
		        "roleName": {
		            remote: "角色名称已经存在"
		        },
		        "roleKey": {
		            remote: "角色权限已经存在"
		        }
		    },
		    focusCleanup: true
		});

		function edit() {
			var roleId = $("input[name='roleId']").val();
			var roleName = $("input[name='roleName']").val();
			var roleKey = $("input[name='roleKey']").val();
			var roleSort = $("input[name='roleSort']").val();
			var status = $("input[id='status']").is(':checked') == true ? 0 : 1;
			var remark = $("input[name='remark']").val();
			var roleRank = $("select[name='roleRank']").val();
			var areaIds = $("#areaIds").val();
			var deptId = $("select[name='deptId']").val();
			// var wmsCode = $("#wmsCode").val();
			var menuIds = $.tree.getCheckedNodes();
			$.ajax({
				cache : true,
				type : "POST",
				url : ctx + "system/role/edit",
				data : {
					"roleId": roleId,
					"roleName": roleName,
					"roleKey": roleKey,
					"roleSort": roleSort,
					"status": status,
					"remark": remark,
					"roleRank": roleRank,
					"areaIds": areaIds,
					// "wmsCode": wmsCode,
					"menuIds": menuIds,
					"deptId": deptId
				},
				async : false,
				error : function(request) {
					$.modal.alertError("系统错误");
				},
				success : function(data) {
					$.operate.successCallback(data);
				}
			});
		}
		
		function submitHandler() {
	        if ($.validate.form()) {
	        	edit();
	        }
	    }
	</script>
</body>
</html>
